<template>
  <div class="login">
    <el-form ref="registerForm" :model="registerForm" :rules="loginRules" class="register-form">
      <h3 class="title">派出单位管理员注册</h3>
      <h3 class="title">{{grantinfo}}</h3>
<!--      <h4 class="title" style="color:red;">-->
<!--      	<a href="https://item.taobao.com/item.htm?id=616173515805" target="_blank">版权所有 侵权必究 授权店铺购买源码</a>-->
<!--      </h4>-->
      <el-form-item prop="username">
        <el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="手机号码">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="registerForm.name" type="text" auto-complete="off" placeholder="姓名">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input style="position: fixed;bottom: -9999px;" class="code_width"></el-input>
        <el-input
          v-model="registerForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="repassword">
        <el-input
          v-model="registerForm.repassword"
          type="password"
          auto-complete="off"
          placeholder="确认密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
<!--      <el-form-item prop="code">-->
<!--        <el-input-->
<!--          v-model="registerForm.code"-->
<!--          auto-complete="off"-->
<!--          placeholder="验证码"-->
<!--          style="width: 63%"-->
<!--          @keyup.enter.native="handleRegister"-->
<!--        >-->
<!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
<!--        </el-input>-->
<!--        <div class="login-code">-->
<!--          <img :src="codeUrl" @click="getCode" />-->
<!--        </div>-->
<!--      </el-form-item>-->
      <el-form-item prop="code">
        <el-input
          v-model="registerForm.smsCode"
          auto-complete="off"
          placeholder="短信验证码"
          style="width: 63%"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="sendSms"
          >
            <span v-if="!loading">获取短信码</span>
          </el-button>
        </div>
      </el-form-item>
      <el-form-item>
      <span><a href="/login">点此跳转登陆页面</a></span>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">注册</span>
          <span v-else>注 册 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2019-2020 <a href="">杭州市教育考试院</a> All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { getGrantInfoById ,getCompanyInfoById,getPhoneCode,companyUserRegister } from '@/api/userregister'
export default {
  name: "userregister",
  data() {
    return {
      grantinfo:"",
      codeUrl: "",
      cookiePassword: "",
      companyId: "",
      grantId: "",
      grantName: "",
      companyName: "",
      registerForm: {
        username: "",
        password: "",
        repassword:"",
        smsCode: "",
        code: "",
        uuid: "",
        comapnyId: undefined,
        grantId: undefined,
        name : ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" }
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" }
        ],
        name: [
          { required: true, trigger: "blur", message: "姓名不能为空" }
        ],
        repassword: [
          { required: true, trigger: "blur", message: "二次密码不能为空" }
        ],
        smsCode: [{ required: true, trigger: "blur", message: "验证码不能为空" }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.grantId = this.$route.query.grantId ? this.$route.query.grantId : '';
    if(this.grantId===''){
      this.$message.error('错误的项目信息')
    }
    this.companyId = this.$route.query.companyId ? this.$route.query.companyId : '';
    if(this.companyId===''){
      this.$message.error('错误的派出单位信息')
    }
    getGrantInfoById({"grantId":this.grantId}).then(res=>{
      if(res.code===200){
        this.grantName = res.data.grantName;
        getCompanyInfoById({"companyId":this.companyId}).then(res=>{
          if(res.code===200){
            this.companyName = res.data.comName;
            this.grantinfo = this.grantName+"("+this.companyName+")";
          }
        })
      }
    })


  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.registerForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.registerForm = {
        username: username === undefined ? this.registerForm.username : username,
        password: password === undefined ? this.registerForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleRegister() {
      // Cookies.set("username", this.registerForm.username, { expires: 30 });
      // Cookies.set("password", encrypt(this.registerForm.password), { expires: 30 });
      if(this.companyId=='')
      {
        this.$message.error('错误的派出单位信息')
         return;
      }
      if(this.grantId=='')
      {
         this.$message.error('错误的项目信息')
         return;
      }
      this.$confirm('是否确认注册'+this.grantinfo+'管理员账号？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        this.$refs.registerForm.validate(valid => {
          if (valid) {
            this.registerForm.grantId = this.grantId;
            this.registerForm.companyId = this.companyId;
            companyUserRegister(this.registerForm).then(res=>{
              if(res.code===200){
                this.$confirm('账号注册成功，是否跳转登陆页面?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'info'
                }).then(() => {
                  this.$router.replace("/login");
                }).catch(() => {
                  this.$message({
                    type: 'info',
                    message: '已取消跳转'
                  });
                });
              }
            })
          }
        });
      }).catch(() => {

      });
      
    },sendSms(){
      if(this.companyId=='')
      {
        this.$message.error('错误的派出单位信息')
         return;
      }
      if(this.grantId=='')
      {
         this.$message.error('错误的项目信息')
         return;
      }
      getPhoneCode(this.registerForm.username).then(res=>{
        if(res.code === 200) {
          this.$message.success("短信已发送，请注意查收");
        }
        else
        {
          this.$message.error(res.msg);
        }
      })
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/image/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  height: 600px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
